import Pagination from "../../modules/pagination/pagination.js";
import $ from "jquery";
import ProfileMgr from "../../data/ProfileMgr.js";
class IndexPagination{
    constructor(showData,cb){
        this.init.apply(this,arguments);
    }
    init(showData,cb){
        this.cb = cb;// 渲染页面模块的回调函数
        this.showData = showData;//每页显示个数
        this.pageCount = 1;//当前页码
        this.totalData = 0;//加载数据的总长度
        this.data = [];//加载回来的数据
        this.dataLoaded = $.Callbacks();
        ProfileMgr.repeatLoad(); 
        this.loadData();
        this.bindEvent();
    }
    bindEvent(){
        this.dataLoaded.add($.proxy(this.getData, this));
        this.dataLoaded.add($.proxy(this.renderPage, this));
        this.dataLoaded.add($.proxy(this.renderBtn, this));
        this.dataLoaded.add($.proxy(this.saveStorage,this));
    }
    // 加载数据
    loadData() {
        $.ajax("./server/index/index_list.json", {
            dataType: "json"
        }).then(this.dataLoaded.fire);
    }
    // 得到所有数据
    getData(res) {
        this.data = res.data;
        this.totalData = this.data.length;
    }
    // 加载完渲染第一页页面数据
    renderPage () {
        this.cb(this.data.slice(this.pageCount - 1, this.showData));
    }
    // 渲染分页按钮
    renderBtn(){
        new Pagination({
            targetCls: ".pageturning .laypage",
            totalData: this.totalData,
            showData: this.showData,
            width: 46,
            callback: function (obj) {
                //重新渲染页面;
                this.pageCount = obj.getCurrent();
                this.cb(this.data.slice(this.showData * (this.pageCount - 1), this.showData * this.pageCount));
            }.bind(this)
        });
    }
    // ??? 到底是保存所有数据还是保存每页数据
    saveStorage(res){
        ProfileMgr.setGoodsList(res.data);
    }
}
module.exports = IndexPagination;